/*
 * Copyright (C) 2010 Ryan Morton
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.games.rm.patternpuzzler;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener
{
	private ImageView splashscreenView;
	private LinearLayout splashscreenLayout;
	
	//main menu
	private LinearLayout mainMenuLayout;
	private TextView resumeGameButton;
	private TextView newGameButton;
	private TextView highScoresButton;
	private TextView howToPlayButton;
	private TextView settingsButton;
	private TextView aboutButton;
	private TextView rateAppButton;
	private Animation mainMenuAnimation;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //set up the window properties
        Utilities.setupActivityWindow(this);
        
        //inflate the main layout
        this.setContentView(R.layout.main);
        
        //run the init();
        this.init();
        
        //check for a savegame
        if(Utilities.savegameExists(this))
        {
        	resumeGameButton.setVisibility(View.VISIBLE);
        }
        
        //check for settings
        if(!Utilities.settingsExists(this))
        {
        	Utilities.resetSettingsToDefault(this);
        }
        
        //show the main menu
        this.showMainMenu();
    }
    
    /***
     * initialize the variables
     */
    private void init()
    {
    	//set up the splashscreen variables******************/
    	splashscreenLayout = (LinearLayout) this.findViewById(R.id.Main_Splashscreen_Layout);
    	splashscreenView = (ImageView) splashscreenLayout.findViewById(R.id.Main_Splashscreen);
    	//set the splashscreen image
        splashscreenView.setImageBitmap(Utilities.getScaledBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.splashscreen),
        		((WindowManager)this.getSystemService(this.WINDOW_SERVICE)).getDefaultDisplay().getWidth(), 0));
        
        //set up the main menu variables******************/
        mainMenuLayout = (LinearLayout)this.findViewById(R.id.Main_Menu_Layout);
        resumeGameButton = (TextView)mainMenuLayout.findViewById(R.id.Main_Menu_Resume_Game_Button);
        newGameButton = (TextView)mainMenuLayout.findViewById(R.id.Main_Menu_New_Game_Button);
        highScoresButton = (TextView)mainMenuLayout.findViewById(R.id.Main_Menu_High_Scores_Button);
        howToPlayButton = (TextView)mainMenuLayout.findViewById(R.id.Main_Menu_How_To_Play_Button);
        settingsButton = (TextView)mainMenuLayout.findViewById(R.id.Main_Menu_Settings_Button);
        aboutButton = (TextView)mainMenuLayout.findViewById(R.id.Main_Menu_About_Button);
        rateAppButton = (TextView)mainMenuLayout.findViewById(R.id.Main_Menu_Rate_Button);
        //setup the main menu animation
        mainMenuAnimation = AnimationUtils.loadAnimation(this, R.anim.slide_in_main_menu);
        mainMenuAnimation.setInterpolator(new BounceInterpolator());
        //set up this as the click listener for the main menu options
        resumeGameButton.setOnClickListener(this);
        newGameButton.setOnClickListener(this);
        highScoresButton.setOnClickListener(this);
        howToPlayButton.setOnClickListener(this);
        settingsButton.setOnClickListener(this);
        aboutButton.setOnClickListener(this);
        rateAppButton.setOnClickListener(this);
    }
    
    /***
     * 
     * @return a new Handler for use with executing threads on/in the GUI thread queue
     */
    private Handler getNewHandler()
	{
		return new Handler();
	}
    
    /***
     * creates a new thread (in the GUI thread queue) that controls when the main menu is displayed.
     * The delay between when the splashscreen is displayed and when the main menu is displayed can
     * be set by changing Utilities.MAIN_MENU_DELAY.
     */
    private void showMainMenu()
    {
    	this.getNewHandler().postDelayed( new Runnable() {
			
			@Override
			public void run()
			{
				mainMenuLayout.startAnimation(mainMenuAnimation);
				mainMenuLayout.setVisibility(View.VISIBLE);
			}
		}, Utilities.MAIN_MENU_DELAY);
    }

	@Override
	public void onClick(View v)
	{
		//check for clicks
		//can use == instead of .equals(...)
		if(v == resumeGameButton)
		{
			if(Utilities.loadSavegame(this).getGameType() == Utilities.GAME_TYPE_4_BUTTON)
			{
				Intent myIntent = new Intent(this, GameBoard4.class);
				//this.startActivityForResult(myIntent, 1);
				this.startActivity(myIntent);
			}
			else
			{
				Intent myIntent = new Intent(this, GameBoard5.class);
				//this.startActivityForResult(myIntent, 1);
				this.startActivity(myIntent);
			}
			
			this.finish();
		}
		else if(v == newGameButton)
		{
			//delete the the savegame if one exists
			if(Utilities.savegameExists(this))
			{
				Utilities.deleteSavegame(this);
			}

			if(Utilities.loadSettings(this).getGameType() == Utilities.GAME_TYPE_4_BUTTON)
			{
				Intent myIntent = new Intent(this, GameBoard4.class);
				//this.startActivityForResult(myIntent, 1);
				this.startActivity(myIntent);
			}
			else
			{
				Intent myIntent = new Intent(this, GameBoard5.class);
				//this.startActivityForResult(myIntent, 1);
				this.startActivity(myIntent);
			}
			
			this.finish();
			
		}
		else if(v == highScoresButton)
		{
			Intent myIntent = new Intent(this, HighScoresActivity.class);
			//this.startActivityForResult(myIntent, 1);
			this.startActivity(myIntent);
		}
		else if(v == howToPlayButton)
		{
			Intent myIntent = new Intent(this, HowToPlayActivity.class);
			//this.startActivityForResult(myIntent, 1);
			this.startActivity(myIntent);
		}
		else if(v == settingsButton)
		{
			Intent myIntent = new Intent(this, SettingsActivity.class);
			//this.startActivityForResult(myIntent, 1);
			this.startActivity(myIntent);
		}
		else if(v == aboutButton)
		{
			Intent myIntent = new Intent(this, AboutActivity.class);
			//this.startActivityForResult(myIntent, 1);
			this.startActivity(myIntent);
		}
		else if(v == rateAppButton)
		{
			this.startActivity( new Intent( Intent.ACTION_VIEW,
					Uri.parse("market://details?id="+Utilities.PACKAGE_NAME) ) );
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if(resultCode == Utilities.FINISH_ACTIVITY)
		{
			this.finish();
		}
		else
		{
			super.onActivityResult(requestCode, resultCode, data);
		}
	}
}